/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022.  All rights reserved.2023.
 */

package service

import (
	"fmt"
	"prim-server/common"
	"prim-server/dao"
	"prim-server/logger"
	"prim-server/model"
	"prim-server/util"
	"time"
)

const (
	defaultUpdateReverseOffset = int32(0)
)

func CreateAndSaveBillUpdatetRecord(userId,
	billUpdateOnePayCycle,
	billUpdateOnePayDateBegin string,
	billUpdateOnePayReverseOffset int32,
	billUpdateOnDemandCycle,
	billUpdateOnDemandDateBegin string,
	billUpdateOnDemandReverseOffset int32,
	resourceUpdateOnePayCycle,
	resourceUpdateOnePayDateBegin string,
	resourceUpdateOnePayReverseOffset int32,
	resourceUpdateOnDemandCycle,
	resourceUpdateOnDemandDateBegin string,
	resourceUpdateOnDemandReverseOffset int32) error {
	record := &model.BillUpdateRecord{
		UserId:                              userId,
		BillUpdateOnePayCycle:               billUpdateOnePayCycle,
		BillUpdateOnePayReverseOffset:       billUpdateOnePayReverseOffset,
		BillUpdateOnePayDateBegin:           billUpdateOnePayDateBegin,
		BillUpdateOnDemandCycle:             billUpdateOnDemandCycle,
		BillUpdateOnDemandReverseOffset:     billUpdateOnDemandReverseOffset,
		BillUpdateOnDemandDateBegin:         billUpdateOnDemandDateBegin,
		ResourceUpdateOnePayCycle:           resourceUpdateOnePayCycle,
		ResourceUpdateOnePayReverseOffset:   resourceUpdateOnePayReverseOffset,
		ResourceUpdateOnePayDateBegin:       resourceUpdateOnePayDateBegin,
		ResourceUpdateOnDemandCycle:         resourceUpdateOnDemandCycle,
		ResourceUpdateOnDemandReverseOffset: resourceUpdateOnDemandReverseOffset,
		ResourceUpdateOnDemandDateBegin:     resourceUpdateOnDemandDateBegin,
	}
	err := dao.GetBillUpdateRecordDao().SaveBillUpdateRecordInfo(record)
	if err != nil {
		logger.Error("create bill updatet record error: %v, userId: %s", err.Error(), userId)
		return err
	}
	return nil
}

func CreateDefaultBillUpdateRecord(userId string) error {
	now := util.GetUTC(time.Now())
	cycle := fmt.Sprintf("%4d-%02d", now.Year(), now.Month())
	offset := defaultUpdateReverseOffset
	dateBegin := fmt.Sprintf("%4d-%02d-%02d", now.Year(), now.Month(), now.Day())
	return CreateAndSaveBillUpdatetRecord(userId, cycle, dateBegin, offset,
		cycle, dateBegin, offset,
		cycle, dateBegin, offset,
		cycle, dateBegin, offset)
}
func GetBillUpdateRecordAll(userId []string) ([]*model.BillUpdateRecord, int, error) {
	queryParamter := &common.QueryBillUpdateRecordDetail{
		UserId: userId,
	}
	records, totalCount, err := dao.GetBillUpdateRecordDao().GetBillUpdateRecordInfo(queryParamter)
	if err != nil {
		logger.Error("get bill update record error: %v, userId: %s", err.Error(), userId)
		return nil, 0, err
	}
	if totalCount == 0 {
		logger.Info("get bill update record: can't find the user %v", userId)
		return nil, 0, nil
	}
	return records, totalCount, nil
}
func GetBillUpdateRecordOneWithUserId(userId string) (*model.BillUpdateRecord, error) {
	records, totalCount, err := GetBillUpdateRecordAll([]string{userId})
	if err != nil {
		logger.Error(err.Error())
		return nil, err
	}
	if totalCount == 0 {
		logger.Info("get bill update record: can't find the user %v", userId)
		return nil, nil
	}
	return records[0], nil
}
func UpdateBillUpdateRecord(recordModify *common.ModifyBillUpdateRecordDetail) error {
	err := dao.GetBillUpdateRecordDao().UpdateBillUpdateRecordInfo(recordModify)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}

// 更新包周期账期和对应账期的reverseOffset
func UpdateOnePayBillRecordInfo(userId string,
	billCycle, billDateBegin *string,
	billReverseOffset *int32) error {
	recordModify := &common.ModifyBillUpdateRecordDetail{
		UserId:                        userId,
		BillUpdateOnePayCycle:         billCycle,
		BillUpdateOnePayReverseOffset: billReverseOffset,
		BillUpdateOnePayDateBegin:     billDateBegin,
	}
	return UpdateBillUpdateRecord(recordModify)
}

// 更新按需账单账期和对应账期的reverseOffset
func UpdateOnDemandBillRecordInfo(userId string,
	billCycle, billDateBegin *string, billReverseOffset *int32) error {
	recordModify := &common.ModifyBillUpdateRecordDetail{
		UserId:                          userId,
		BillUpdateOnDemandCycle:         billCycle,
		BillUpdateOnDemandReverseOffset: billReverseOffset,
		BillUpdateOnDemandDateBegin:     billDateBegin,
	}
	return UpdateBillUpdateRecord(recordModify)
}

func UpdateBillRecordInfo(userId, queryChargeMode string,
	billCycle, billDateBegin *string, billReverseOffset *int32) (err error) {
	if IsChargeModeOfOnePay(queryChargeMode) {
		err = UpdateOnePayBillRecordInfo(userId, billCycle, billDateBegin, billReverseOffset)
	} else if IsChargeModeOfOnDemand(queryChargeMode) {
		err = UpdateOnDemandBillRecordInfo(userId, billCycle, billDateBegin, billReverseOffset)
	}
	if err != nil {
		logger.Error("update bill record error: %v, userId: %s, chargeMode: %s", err.Error(), userId, queryChargeMode)
		return err
	}
	return nil
}

// 更新包周期账期和对应账期的资源消费记录的reverseOffset
func UpdateOnePayResourceRecordInfo(userId string,
	billCycle, billDateBegin *string, resourceRecordReverseOffset *int32) error {
	recordModify := &common.ModifyBillUpdateRecordDetail{
		UserId:                            userId,
		ResourceUpdateOnePayCycle:         billCycle,
		ResourceUpdateOnePayReverseOffset: resourceRecordReverseOffset,
		ResourceUpdateOnePayDateBegin:     billDateBegin,
	}
	return UpdateBillUpdateRecord(recordModify)
}

// 更新按需账单账期和对应账期的资源消费记录的reverseOffset
func UpdateOnDemandResourceRecordInfo(userId string,
	billCycle, billDateBegin *string, resourceRecordReverseOffset *int32) error {
	recordModify := &common.ModifyBillUpdateRecordDetail{
		UserId:                              userId,
		ResourceUpdateOnDemandCycle:         billCycle,
		ResourceUpdateOnDemandReverseOffset: resourceRecordReverseOffset,
		ResourceUpdateOnDemandDateBegin:     billDateBegin,
	}
	return UpdateBillUpdateRecord(recordModify)
}

func UpdateResourceRecordInfo(userId, queryChargeMode string,
	billCycle, billDateBegin *string, resourceRecordReverseOffset *int32) (err error) {
	if IsChargeModeOfOnePay(queryChargeMode) {
		err = UpdateOnePayResourceRecordInfo(userId, billCycle, billDateBegin, resourceRecordReverseOffset)
	} else if IsChargeModeOfOnDemand(queryChargeMode) {
		err = UpdateOnDemandResourceRecordInfo(userId, billCycle, billDateBegin, resourceRecordReverseOffset)
	}
	if err != nil {
		logger.Error("update resource record error: %v, userId: %s, chargeMode: %s", err.Error(), userId, queryChargeMode)
		return err
	}
	return nil
}
